package com.hs.android.fortress.intro;

import java.util.List;

import org.apache.http.NameValuePair;

import com.google.android.gcm.GCMRegistrar;
import com.google.android.gms.internal.ay;
import com.hs.android.fortress.GameApplication;
import com.hs.android.fortress.R;
import com.hs.android.fortress.model.Player;
import com.hs.android.fortress.net.AsyncNetworking;
import com.hs.android.fortress.net.FormPostCommand;
import com.hs.android.fortress.net.NetworkCommand;
import com.hs.android.fortress.net.ParamsMaker;
import com.hs.android.fortress.net.AsyncNetworking.onNetworkProcessListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

public class WaitingRoomActivitiy extends Activity {

	private static final String TAG = WaitingRoomActivitiy.class.getSimpleName();

	public static final String ENTER_ROOM_ACTION = "com.hs.android.fortress.ENTER_ROOM";

	private WaitingRoomFragment mWaitingRoomFragment;
	private String mUserId;
	private int roomId;
	
	private AlertDialog mQuitDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_room);

		GameApplication app = (GameApplication) getApplication();
		mUserId = app.getUserId();
		
		Intent i = getIntent();
		roomId = i.getIntExtra(WaitingRoomFragment.EXTRA_ROOM_NUMBER, 0);

		FragmentManager fm = getFragmentManager();
		mWaitingRoomFragment = (WaitingRoomFragment) fm.findFragmentById(R.id.room_container);

		if (mWaitingRoomFragment == null) {
			mWaitingRoomFragment = WaitingRoomFragment.newInstance(roomId);
			FragmentTransaction ft = fm.beginTransaction();
			ft.add(R.id.room_container, mWaitingRoomFragment).commit();
		}

	}

	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			mQuitDialog = new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle("Quit")
			.setMessage("Do you want to quit?")
			.setPositiveButton("Yes",mPositveListener)
			.setNegativeButton("No", null)
			.setCancelable(false)
			.create();
			
			mQuitDialog.show();
			
			return true;
		}

		return super.onKeyDown(keyCode, event);
	};
	
	private OnClickListener mPositveListener = new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			NetworkCommand existCommand = new FormPostCommand("/room/exit", ParamsMaker.makeIdNumberParams(mUserId, roomId));
			AsyncNetworking.newInstance(WaitingRoomActivitiy.this,existCommand, mExistListener).execute();
		}
	};
	
	private onNetworkProcessListener mExistListener = new onNetworkProcessListener() {
		
		@Override
		public void onPostExecute(String result) {
			if(result == null){
				AsyncNetworking.makeNetworkProblemDialog(WaitingRoomActivitiy.this)
					.setPositiveButton("OK", null)
					.create().show();
			}else if(result.equals("exited")){
				mQuitDialog.dismiss();
				WaitingRoomActivitiy.this.finish();
			}
		}
	};

}
